﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Msung.Core.Caching;
using Msung.Core.Freesql;
using Msung.Core.Kafka;
using Msung.Core.Kafka.Integration;
using Msung.Core.Model;
using Msung.Core.Snowflake;

namespace Msung.Core
{

    public static class AppSetting
    {
        public static IServiceCollection _Services;
        public static T GetService<T>()
        {
            var provider = _Services.BuildServiceProvider();
            return provider.GetRequiredService<T>();

        }
        #region  静态 Configuration 用来获取未配置的节点
        public static IConfiguration Configuration { get; private set; }

        public static string AppName { get; set; }
        public static KafkaBaseOptions KafkaBaseOptions { get; set; }

        public static RedisOptions RedisOptions { get; set; }
        public static List<KafkaConsumerOptions> KafkaConsumerOptions { get; set; }
        public static List<KafkaProducerOptions> KafkaProducerOptions { get; set; }

        public static List<JobOption> JobOptions { get; set; }

        /// <summary>
        ///数据库连接配置
        /// </summary>
        public static List<MultiDb> MultiDbs { get; set; }
        public static List<MultiNoSqlDb> MultiNoSqlDbs { get; set; }
        // 多个节点name格式 ：["key:key1"]
        public static string GetSettingString(string key)
        {
            return Configuration[key];
        }
        // 多个节点,通过.GetSection("key")["key1"]获取
        public static IConfigurationSection GetSection(string key)
        {
            return Configuration.GetSection(key);
        }
        #endregion
        public static void BuildConfiguration(this IServiceCollection services)
        {
            _Services = services;
            var provider = services.BuildServiceProvider();
            Configuration = provider.GetRequiredService<IConfiguration>();
            AppName = GetSettingString("AppName");
            services.Configure<SnowflakeIdOptions>(Configuration.GetSection("SnowflakeId"));
            services.Configure<List<MultiDb>>(Configuration.GetSection("Dbs"));
            services.Configure<List<MultiNoSqlDb>>(Configuration.GetSection("NoSqlDbs"));
            services.Configure<RedisOptions>(Configuration.GetSection("RedisOptions"));
            services.Configure<KafkaBaseOptions>(Configuration.GetSection("KafkaBaseOptions"));
            services.Configure<List<KafkaConsumerOptions>>(Configuration.GetSection("KafkaConsumerOptions"));
            services.Configure<List<KafkaProducerOptions>>(Configuration.GetSection("KafkaProducerOptions"));
            services.Configure<List<JobOption>>(Configuration.GetSection("JobOptions"));
            provider = services.BuildServiceProvider();
            MultiDbs = provider.GetRequiredService<IOptions<List<MultiDb>>>().Value;
            MultiNoSqlDbs = provider.GetRequiredService<IOptions<List<MultiNoSqlDb>>>().Value;
            ///redis连接
            RedisOptions = provider.GetRequiredService<IOptions<RedisOptions>>().Value;
            ///kafka连接
            KafkaBaseOptions = provider.GetRequiredService<IOptions<KafkaBaseOptions>>().Value;
            ///消费者
            KafkaConsumerOptions = provider.GetRequiredService<IOptions<List<KafkaConsumerOptions>>>().Value;
            ///生产者
            KafkaProducerOptions = provider.GetRequiredService<IOptions<List<KafkaProducerOptions>>>().Value;
            ///Sql后执行
            JobOptions = provider.GetRequiredService<IOptions<List<JobOption>>>().Value;
        }

    }
}


